package com.hongxun.budget.ui.activity.ys;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.hongxun.budget.R;
import com.hongxun.budget.ui.activity.BaseActivity;
import com.hongxun.budget.utils.T;
import com.hongxun.budget.utils.TestingUtils;
import com.hongxun.greenentity.ys.EDeviceBuyEntity;

import butterknife.BindView;
import butterknife.OnClick;

public class YsEditDeviceActivity extends BaseActivity {


    @BindView(R.id.right_save)
    Button rightSave;
    @BindView(R.id.name)
    EditText name;
    @BindView(R.id.parameter)
    EditText parameter;
    @BindView(R.id.price)
    EditText price;
    @BindView(R.id.num)
    EditText num;

    EDeviceBuyEntity d;
    int index = 0;

    @Override
    public  int getLayoutId() {
        return R.layout.activity_edit_device;
    }

    @Override
    public  void initView(Bundle savedInstanceState) {
        Intent intent = getIntent();
        index = intent.getIntExtra("index", 0);
        d = (EDeviceBuyEntity) intent.getSerializableExtra("device");
        rightSave.setVisibility(View.VISIBLE);
        name.setText(d.getDBName());
        parameter.setText(d.getDBParameter());
        price.setText((d.getDBPrice() == null ? 0 : d.getDBPrice()) + "");
        num.setText(Integer.parseInt(d.getDBCount().toString()) + "");
    }


    @OnClick(R.id.right_save)
    public void onViewClicked() {
        String n = getText(name);
        String p = getText(parameter);
        String m = getText(num);
        String c = getText(price);
        if (TextUtils.isEmpty(m) || Integer.parseInt(m) == 0) {
            showMsg("购置数量不能为空或0");
            return;
        }
        if (TextUtils.isEmpty(c) || Double.parseDouble(c) == 0) {
            showMsg("购置单价不能为空或0");
            return;
        }
        if (!TestingUtils.isEmpty(context, n, "名称")) return;
        if (!TestingUtils.isEmpty(context, p, "技术参数")) return;
        d.setDBName(n);
        d.setDBPrice(Double.parseDouble(c));
        d.setDBCount(Integer.parseInt(m));
        d.setDBParameter(p);
        d.setDBPriceall(d.getDBPrice() * d.getDBCount());
        d.setDBDatastate("修改");
        Intent intent = new Intent();
        intent.putExtra("device", d);
        intent.putExtra("index", index);
        setResult(Activity.RESULT_OK, intent);
        finish();
        T.showLong(context, "保存成功");
    }
}
